﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HUGATE.ItemObject;
using CoreLibs;
using HUGATE.Utility;

/// <summary>
///  Creator : Nguyễn Đức Hiếu
/// Date    : 24/12/2010
/// Desc    : Quản lý giá trị sản phẩm
/// </summary>
namespace HUGATE.Item.Controls.ProductValue
{
    public partial class ProductValueList : System.Web.UI.UserControl
    {
        #region Variables

        private int iPageSize = 10;
        private string strReturnPage = "";

        #endregion

        #region Properties

        public int PageSize
        {
            get { return iPageSize; }
            set { iPageSize = value; }
        }

        #endregion

        #region Permissions

        //private String strManagerPermission = "ITEM_Unit_Manager";
        //private String strSystemPermission = "ITEM_Unit_System";

        #endregion

        #region Support Functions

        /// <summary>
        /// Nạp danh sách dữ liệu vào lưới
        /// </summary>
        /// <param name="intPage"></param>
        private void GridDatabind(int intPage)
        {
            ITEMProductValue objValue = new ITEMProductValue();
            DataTable dtb = objValue.SearchData(txtSearch.Text.Trim());

            grvObject.DataSource = dtb;
            grvObject.PageIndex = intPage;
            grvObject.DataBind();
        }

        #endregion

        #region Event Functions

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtSearch.MaxLength = 200;
                btnAddNew.Text = HUGATE.Utility.ResxManager.GetString("Btn_AddNew");
                btnReturn.Text = HUGATE.Utility.ResxManager.GetString("Btn_Return");
                btnSearch.Text = HUGATE.Utility.ResxManager.GetString("Btn_Search");

                grvObject.Columns[0].HeaderText = HUGATE.Utility.ResxManager.GetString("ID") + HUGATE.Utility.ResxManager.GetString("Value");
                grvObject.Columns[1].HeaderText = HUGATE.Utility.ResxManager.GetString("Name") + HUGATE.Utility.ResxManager.GetString("Value");
                grvObject.Columns[2].HeaderText = HUGATE.Utility.ResxManager.GetString("ID") + HUGATE.Utility.ResxManager.GetString("Attribute");
                grvObject.Columns[3].HeaderText = HUGATE.Utility.ResxManager.GetString("Description");
                grvObject.Columns[4].HeaderText = HUGATE.Utility.ResxManager.GetString("Default");
                grvObject.Columns[5].HeaderText = HUGATE.Utility.ResxManager.GetString("Actived");
                

                grvObject.EmptyDataText = string.Format(HUGATE.Utility.ResxManager.GetString("DetailItemNotFound"), HUGATE.Utility.ResxManager.GetString("Value", true));

                GridDatabind(0);
            }
            ctrlProductValueEdit.SavedEvent += new ProductValueEdit.SaveDelegate(ctrlProductValueEdit_SavedEvent);
            btnReturn.OnClientClick = "window.location='" + Globals.ApplicationVRoot() + "/" + strReturnPage + "';return false;";
        }

        void ctrlProductValueEdit_SavedEvent(object sender, EventArgs e)
        {
            mpeUpdate.Hide();
            GridDatabind(0);
            udpList.Update();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GridDatabind(0);
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            mpeUpdate.Show();
            udpEdit.Update();
            lblTitle.Text = HUGATE.Utility.ResxManager.GetString("PopupTitle_Insert");
        }


        protected void grvObject_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridDatabind(e.NewPageIndex);
        }

        protected void grvObject_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblTitle.Text = HUGATE.Utility.ResxManager.GetString("PopupTitle_Update");
            ctrlProductValueEdit.ValueID = Convert.ToInt32(grvObject.SelectedValue);
            ctrlProductValueEdit.LoadInfo();
            mpeUpdate.Show();
            udpEdit.Update();
        }
        protected void lbtEdit_Click(object sender, EventArgs e)
        {
            LinkButton lbtemp = sender as LinkButton;
            ITEMProductValue objProdcutValue = new ITEMProductValue();
            ITEMProductAttribute objProductAttribute = new ITEMProductAttribute();
            DataTable objdtValue = objProdcutValue.SearchData("");
            DataTable objdtAttribute = objProductAttribute.SearchData("");
            DataRow objRow = objdtValue.Select("ValueID = " + lbtemp.CommandArgument)[0];
            Session["IDAttribute"] = objRow["AttributeID"].ToString();
            ctrlProductValueEdit.LoadInfo();
        }

        protected void imgDelete_Click(object sender, EventArgs e)
        {
            LinkButton lbtTemp = sender as LinkButton;
            ITEMProductValue objValue = new ITEMProductValue();

            objValue.ValueID= Convert.ToInt32(lbtTemp.CommandArgument);
            objValue.CreatedUser = "administrator";//User.UserName;

            if (objValue.Delete())
            {
                GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("DeleteItemSuccessful"), HUGATE.Utility.ResxManager.GetString("ITEMProductValue", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.Information);
                GridDatabind(grvObject.PageIndex);
            }
            else
                GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("CouldNotDeleteParent"), HUGATE.Utility.ResxManager.GetString("ITEMProductValue", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.WarningMessage);

        }

        #endregion

        
    }
}